<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

$user_id 	= ( ! empty($_GET['id'])) ? trim(mysql_real_escape_string($_GET['id'])) : $_SESSION['uid'];
$act		= ( ! empty($_GET['act'])) ? (int)$_GET['act'] : 0;
$msg_box	= '';

switch($act)
{
	// L'oggetto viene comprato correttamente
	case 1:
		$msg_box .= '<div class="alert alert-success">';
		$msg_box .= 'Oggetto acquistato correttamente.';
		$msg_box .= '</div>';
	break;
	// L'oggetto viene venduto correttamente
	case 2:
		$msg_box .= '<div class="alert alert-success">';
		$msg_box .= 'Oggetto venduto correttamente.';
		$msg_box .= '</div>';
	break;
}

//Variabili
$money		= "";
$itemlist	= "";

// Recupera i soldi del personaggio
if($r1 = $db -> query("SELECT user_money FROM users WHERE user_id = '$user_id'"))
{
	$rd1 = $r1 -> fetch_assoc();
	
	$money = $rd1['user_money'];
	$format_money = number_format($money, 0, ',', '.');
}

// Recupera l'elenco degli oggetti del mercato
if($r2 = $db -> query("SELECT * FROM item_categories"))
{
	
	$itemlist .= '<div class="panel-group" id="market">';
	
	$n = 0;
	while($rd2 = $r2 -> fetch_assoc())
	{
		$category_id	= $rd2['item_category_id'];
		$category_name	= $rd2['item_category_name'];
				
		$itemlist .= '<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title">';
		$itemlist .= '<a data-toggle="collapse" data-parent="#market" href="#category' . $n . '">' . $category_name . '</a></h4></div>';
		
		// Recupera i dati dell'oggetto
		$q3 	= "SELECT b.item_id item_id, b.item_name item_name, b.item_number item_number, b.item_price item_price, b.item_longdesc item_longdesc,
				IF(il.user_id IS NULL, 0, il.user_id) item_user, 
				IF(il.item_number IS NULL, 0, il.item_number) user_n_item
				FROM (
				SELECT * FROM (
				SELECT m.item_id, m.item_number, m.item_price, i.item_name, i.item_longdesc, i.category_id
				FROM market m
				LEFT JOIN items i ON m.item_id = i.item_id
				) mi
				) AS b
				LEFT JOIN itemslink il ON il.item_id = b.item_id
				WHERE b.category_id = $category_id
				ORDER BY b.item_name";
						
		if($r3 = $db -> query($q3)) 
		{
			$itemlist .= '<div id="category' . $n . '" class="panel-collapse collapse">';
			$itemlist .= '<div class="panel-body">';
			while($rd3 = $r3 -> fetch_assoc())
			{
			
				$iid			= $rd3['item_id'];
				$inum			= $rd3['item_number'];
				$iprice			= $rd3['item_price'];
				$uitem			= $rd3['item_user'];
				$unitem			= $rd3['user_n_item'];
				$iname			= trim(htmlentities($rd3['item_name']));
				$idesc			= trim(htmlentities($rd3['item_longdesc']));
				
				// Se � presente almeno un oggetto, calcola il prezzo
				if ($inum != 0)
				{
					$iprice = round($iprice);
				}
				
				$itemlist .= '<div class="media"><div class="pull-left"><img class="media-object" src="img/locations/default.jpg" title="' . $iname . '"></div>';
				$itemlist .= '<div class="media-body"><h5 class="media-heading">' . $iname . '</h5>';
				$itemlist .= '<p>' . $idesc . '<hr /><b>Quantit&agrave;: </b>' . $inum . '<br /><b>Prezzo: </b>' . $iprice . '</p>';
				if (($money >= $iprice) AND ($inum > 0))
				{
					$itemlist .= '<form class="form-inline pull-left" action="market-buy.php" method="post" role="form">';
					$itemlist .= '<input type="hidden" name="item" value="' . $iid . '">';
					$itemlist .= '<input type="hidden" name="money" value="' . $money . '">';
					$itemlist .= '<input type="hidden" name="act" value="buy">';
					$itemlist .= '<div class="form-group"><select name="buyItemNum" class="form-control">';
					for($n = 0; $n <= $inum; $n++)
					{
						$itemlist .= '<option value="' . $n . '">' . $n . '</option>';
					}
					$itemlist .= '</select></div>';
					$itemlist .= '<button type="submit" class="btn btn-success">Compra</button>';
					$itemlist .= '</form>';
				}
				if($unitem > 0 AND $uitem = $user_id)
				{
					$itemlist .= '<form class="form-inline pull-right" action="market-buy.php" method="post" role="form">';
					$itemlist .= '<input type="hidden" name="item" value="' . $iid . '">';
					$itemlist .= '<input type="hidden" name="money" value="' . $money . '">';
					$itemlist .= '<input type="hidden" name="act" value="sell">';
					$itemlist .= '<div class="form-group"><select name="sellItemNum" class="form-control">';
					for($n = 0; $n <= $unitem; $n++)
					{
						$itemlist .= '<option value="' . $n . '">' . $n . '</option>';
					}
					$itemlist .= '</select></div>';
					$itemlist .= '<button type="submit" class="btn btn-danger">Vendi</button>';
					$itemlist .= '</form>';
				}
				$itemlist .= '</div></div>';
				
				$n++;
				
				
			}
			$itemlist .= '</div></div>';
		}
	}
	$itemlist .= '</div>';
}



// Lista delle tag speciali
$tags = array('temp_title' => $g_name,'temp_currency' => $g_currency, 'temp_itemlist' => $itemlist, 'temp_money' => $format_money);

		
// Carica il template e formatta le tag speciali
$temp = new template('style/market.template.html');
$temp -> replace($tags);
	
// Visualizza l'output
echo $temp -> show();

// Chiude la connessione al database
$db -> close();

?>